<template>
  <div class='container'>
    <el-card class="box-card">

      <!-- 筛选区域 -->
      <questions-filter ref="filter" @search="loadQuestionsList($event)"/>
      <!-- 筛选区域/ -->

      <!-- 提示 -->
        <el-alert
          :title="'数据一共 '+ total +' 条'"
          type="info"
          show-icon
          :closable="false"
          style="margin-bottom:20px;"
          >
        </el-alert>
      <!-- 提示/ -->

      <!-- 表格内容 -->
        <el-table
          :data="questionsList"
          :header-cell-style="tableHeaderStyle"
          style="width: 100%">
          <el-table-column
            prop="number"
            label="试题编号"
            width="130px"
            >
          </el-table-column>
          <el-table-column
            prop="subject"
            label="学科"
            >
          </el-table-column>
          <el-table-column
            prop="catalog"
            label="目录"
            >
          </el-table-column>
          <el-table-column
            prop="questionType"
            label="题型"
            >
            <template slot-scope="{ row }">
              {{ showQuestionType(row.questionType) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="question"
            label="题干"
            >
            <template slot-scope="{ row }">
              <span v-html="row.question"></span>
            </template>
          </el-table-column>
          <el-table-column
            prop="addDate"
            label="录入时间"
            >
            <template slot-scope="{ row }">
              {{ row.addDate | parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column
            prop="difficulty"
            label="难度"
            >
            <template slot-scope="{ row }">
              {{ showDifficulty(row.difficulty) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="creator"
            label="录入人"
            >
          </el-table-column>
          <el-table-column
            label="操作"
            width="200px"
            >
            <template slot-scope="{ row }">
              <el-button
                type="primary"
                icon="el-icon-view"
                circle
                plain
                title="预览"
                @click="onView(row.id)"
              ></el-button>
              <el-button
                type="success"
                icon="el-icon-edit"
                circle
                plain
                title="修改"
                @click="onEdit(row.id)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                plain
                title="删除"
                @click="onDelete(row.id)"
              ></el-button>
              <el-button
                type="warning"
                icon="el-icon-check"
                circle
                plain
                title="加入精选"
                @click="onCollect(row.id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      <!-- 表格内容/ -->

      <!-- 分页 -->
      <el-row>
        <el-col :span="6" :offset="17" style="margin-top:20px;">
          <page-tool
          :paginationPage="page"
          :paginationPagesize="pagesize"
          :total="total"
          @pageChange="onPageChange"
          @pageSizeChange="onPageSizeChange"
          />
        </el-col>
      </el-row>
      <!-- 分页/ -->
    </el-card>

    <!-- 题目预览弹窗 -->
      <questions-preview v-model="previewVisible" ref="preview"/>
    <!-- 题目预览弹窗/ -->
  </div>
</template>

<script>
import questionsFilter from '../components/questions-filter.vue'
import { list as questionsList, remove as removeQuestion, choiceAdd } from '@/api/hmmm/questions.js'
import PageTool from '../../module-dashboard/components/pageTool.vue'
import QuestionsPreview from '../components/questions-preview.vue'
import { showQuestionType, showDifficulty } from '@/filters/questions.js'
export default {
  components: { questionsFilter, PageTool, QuestionsPreview },
  data () {
    return {
      page: 1,
      pagesize: 5,
      total: 0,
      questionsList: [],
      previewVisible: false
    }
  },
  created () {
    this.loadQuestionsList()
  },
  methods: {
    // 加载基础题库列表
    async loadQuestionsList (queryData) {
      const { data } = await questionsList({
        page: this.page,
        pagesize: this.pagesize,
        ...queryData
      })
      this.questionsList = data.items
      this.total = data.counts
    },

    // 分页页码变化
    onPageChange (val) {
      this.page = val
      const queryData = this.$refs.filter.form
      this.loadQuestionsList(queryData)
    },

    // 分页数量变化
    onPageSizeChange (val) {
      this.pagesize = val
      const queryData = this.$refs.filter.form
      this.loadQuestionsList(queryData)
    },
    showQuestionType,
    showDifficulty,
    // 点击预览
    onView (id) {
      this.$refs.preview.loadQuestionsDetails({ id })
      this.previewVisible = true
    },

    // 点击修改
    onEdit (id) {
      this.$router.push({
        path: 'new',
        query: {
          id
        }
      })
    },

    // 点击删除
    onDelete (id) {
      this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await removeQuestion({ id })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.loadQuestionsList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 点击加入精选
    onCollect (id) {
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(async () => {
        await choiceAdd({
          id,
          choiceState: 1
        })
        this.$message({
          type: 'success',
          message: '加入精选成功!'
        })
        this.loadQuestionsList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消加入精选'
        })
      })
    },

    // 表头高亮
    tableHeaderStyle ({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background:#F5F7FA;color:#1f2d3d'
      } else {
        return ''
      }
    }
  }
}
</script>

<style scoped lang='less'>
.box-card {
  margin: 20px;
  /deep/.el-table th.el-table__cell>.cell{
  color: #909399;
  }
}
</style>
